﻿using DataVirtualization.Toolkit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Wimm.Common.Entities.Utils;
using Wimm.Common;
using Windows.ApplicationModel.Resources.Core;
using Windows.UI.Xaml;

namespace Wimm.CustomContols
{
    public class FilterCategory
    {
        public int Key { get; private set; }
        public string Value { get; private set; }
        public double Opacity { get; set; }
        public FilterCategory(int key, string value, bool isShow)
        {
            this.Key = key;
            this.Value = value;
            this.SetOpacity(isShow);
        }
        public void SetOpacity(bool isShow)
        {
            this.Opacity = isShow ? 1.0 : 0.4;
        }
    }
    public class FilterData : INotifyPropertyChanged
    {
        private TimePeriod _selectedTimePeriod;
        private DateTimeOffset _fromFilter;
        private DateTimeOffset _toFilter;
        private FilterCategory _selectedcategory;
        private bool _detailed;

        public bool Detailed
        {
            get { return _detailed; }
            set 
            {
                ApplyCommand.Execute(null);
                _detailed = value; 
            }
        }
        public List<FilterCategory> Categories { get; set; }
        public FilterCategory SelectedCategory
        {
            get { return _selectedcategory; }
            set
            {
                ApplyCommand.Execute(null);
                _selectedcategory = value;
            }
        }
        public IEnumerable<TimePeriod> TimePeriods { get; set; }
        public DateTimeOffset FromFilter
        {
            get { return _fromFilter; }
            set
            {
                _selectedTimePeriod = TimePeriods.ElementAt(0);
                OnPropertyChanged("SelectedTimePeriod");
                ApplyCommand.Execute(null);
                _fromFilter = value;
            }
        }
        public DateTimeOffset ToFilter
        {
            get { return _toFilter; }
            set
            {
                _selectedTimePeriod = TimePeriods.ElementAt(0);
                OnPropertyChanged("SelectedTimePeriod");
                ApplyCommand.Execute(null);
                _toFilter = value;
            }
        }
        public TimePeriod SelectedTimePeriod 
        {
            get { return _selectedTimePeriod; }
            set 
            {
                if (value.Name != "FilterCustom")
                {
                    _fromFilter = value.Start;
                    _toFilter = value.End;
                    ApplyCommand.Execute(null);
                    OnPropertyChanged("FromFilter");
                    OnPropertyChanged("ToFilter");
                }
                _selectedTimePeriod = value;
            }
        }
        public ICommand ApplyCommand { get; set; }

        public FilterData(DateTimeOffset fromFilter, DateTimeOffset toFilter,
            IEnumerable<TimePeriod> timePeriods, string lang, ICommand applyCommand)
        {
            this.ApplyCommand = applyCommand;
            this.TimePeriods = timePeriods;
            this.FromFilter = fromFilter;
            this.ToFilter = toFilter;
            this.SelectedTimePeriod = timePeriods.ElementAt(1);
            this.Categories = new List<FilterCategory>();
            this.Categories.Add(new FilterCategory(0, "All".GetStringFromResource(lang), true));
            this.SelectedCategory = this.Categories.ElementAt(0);
        }

        public void SetAllCategories(IEnumerable<Tuple<int, int, int, string>> categories, string lang, bool update = true)
        {   // Tuple<id, parent id, nesting lvl, name>
            this.Categories.Clear();
            this.Categories.Add(new FilterCategory(0, "All".GetStringFromResource(lang), true));
            foreach (var item in categories)
            {
                string s = string.Empty;
                for (int i = 0; i < item.Item3; i++) { s += "-"; }

                string description = item.Item4.GetStringFromResource(lang);

                this.Categories.Add(new FilterCategory(item.Item1, s + description, false));
            }
            if (update)
            {
                this.SelectedCategory = this.Categories.ElementAt(0);
            }
            else 
            {
                this._selectedcategory = this.Categories.ElementAt(0);
            }
        }

        public void SetOpacityCategories(IEnumerable<int> enumerable)
        {
            var count = this.Categories.Count();
            for (int i = 1; i < count; i++)
            {
                this.Categories[i].SetOpacity(enumerable.Any(x => x == this.Categories[i].Key));
            }
        }

        public void SetFilterLang(string lang) 
        {
            this.TimePeriods = TimePeriod.GetConfiguration(lang);
            _selectedTimePeriod = this.TimePeriods.ElementAt(1);
        }

        #region INotifyPropertyChanged
        public void OnPropertyChanged(string args)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(args));
        }

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion    
    }
}
